from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import  Keys
import time
import csv
#浏览器驱动
#https://npm.taobao.org/mirrors/chromedriver

#加载驱动
web = Chrome()
#获取网址
web.get("http://www.lagou.com")
#点击“全国”
web.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a').click()
#怕未加载，自动睡眠1s
time.sleep(1)
#搜索“python”并回车
web.find_element_by_xpath('//*[@id="search_input"]').send_keys("python", Keys.ENTER)
#得到所以li职位信息  experience  经验 company 公司
list=web.find_elements_by_xpath('//*[@id="s_position_list"]/ul/li')
file = open("resource\数据.csv",mode="w",encoding="utf-8")
w = csv.writer(file)
for i in list:
    job_name = i.find_element_by_tag_name('h3').text
    company = i .find_element_by_xpath('./div[1]/div[2]/div[1]/a').text
    experience = i.find_element_by_xpath('./div[1]/div[1]/div[2]/div').text
    price = i.find_element_by_xpath('./div[1]/div[1]/div[2]/div/span').text
    text =job_name+company+experience+price
    dic = []
    dic.append(text)
    w.writerow(dic)
file.close()
web.quit()